#include "ActivityEightActivityEightDesc.h"
#include "NFComm/NFPluginModule/NFCheck.h"
#include "NFComm/NFCore/NFFileUtility.h"
#include "ActivityEightDescEx.h"

ActivityEightActivityEightDesc::ActivityEightActivityEightDesc()
{
	if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode()) {
		CreateInit();
	}
	else {
		ResumeInit();
	}
}

ActivityEightActivityEightDesc::~ActivityEightActivityEightDesc()
{
}

int ActivityEightActivityEightDesc::CreateInit()
{
	return 0;
}

int ActivityEightActivityEightDesc::ResumeInit()
{
	return 0;
}

int ActivityEightActivityEightDesc::LoadDB(NFResDb *pDB)
{
	CHECK_EXPR(pDB != NULL, -1, "pDB == NULL");

	NFLogTrace(NF_LOG_DEFAULT, 0, "ActivityEightActivityEightDesc::LoadDB() strFileName = {}", GetFileName());

	proto_ff::Sheet_ActivityEightActivityEight table;
	NFResTable* pResTable = pDB->GetTable(GetFileName());
	CHECK_EXPR(pResTable != NULL, -1, "pTable == NULL, GetTable:{} Error", GetFileName());

	int iRet = 0;
	iRet = pResTable->FindAllRecord(GetDBName(), &table);
	CHECK_EXPR(iRet == 0, -1, "FindAllRecord Error:{}", GetFileName());

	//NFLogTrace(NF_LOG_DEFAULT, 0, "Load From DB:{}", table.Utf8DebugString());

	std::string bin_file = GetFilePathName();

	std::string content = table.SerializePartialAsString();

	NFFileUtility::WriteFile(bin_file, content);

	NFJson2PB::Pb2JsonOptions options;
	options.pretty_json = true;
	std::string excelJson;
	std::string excelJsonError;
	if (!NFProtobufCommon::ProtoMessageToJson(options, table, &excelJson, &excelJsonError))
	{
		CHECK_ERR(0, -1, "ProtoMessageToJson Failed table:{} errorJson:{}", GetFileName(), excelJsonError);
	}
	std::string debug_file = NFFileUtility::GetExcludeFileExt(bin_file) + "_debug.json";

	NFFileUtility::WriteFile(debug_file, excelJson);

	SetDBLoaded(true);
	return 0;
}

int ActivityEightActivityEightDesc::Load(NFResDb *pDB)
{
	CHECK_EXPR(pDB != NULL, -1, "pDB == NULL");

	NFLogTrace(NF_LOG_DEFAULT, 0, "ActivityEightActivityEightDesc::Load() strFileName = {}", GetFileName());

	proto_ff::Sheet_ActivityEightActivityEight table;
	NFResTable* pResTable = pDB->GetTable(GetFileName());
	CHECK_EXPR(pResTable != NULL, -1, "pTable == NULL, GetTable:{} Error", GetFileName());

	int iRet = 0;
	iRet = pResTable->FindAllRecord(GetDBName(), &table);
	CHECK_EXPR(iRet == 0, -1, "FindAllRecord Error:{}", GetFileName());

	//NFLogTrace(NF_LOG_DEFAULT, 0, "{}", table.Utf8DebugString());

	if ((table.e_activityeightactivityeight_list_size() < 0) || (table.e_activityeightactivityeight_list_size() > (int)(m_astDescVec.max_size())))
	{
		NFLogError(NF_LOG_DEFAULT, 0, "Invalid TotalNum:{}", table.e_activityeightactivityeight_list_size());
		return -2;
	}

	if (IsReloading())
	{
		std::set<int64_t> newKeys;
		for (int i = 0; i < (int)table.e_activityeightactivityeight_list_size(); i++)
		{
			const proto_ff::E_ActivityEightActivityEight& desc = table.e_activityeightactivityeight_list(i);
			if ((desc.has_id() == false && desc.ByteSize() == 0) || desc.id() < 0)
			{
				continue;
			}
			newKeys.insert(desc.id());
			if (m_astDescMap.find(desc.id()) == m_astDescMap.end())
			{
				NFLogInfo(NF_LOG_DEFAULT, 0, "reload add new col, id:{}",  desc.id());
			}
		}
		for (auto iter = m_astDescMap.begin(); iter != m_astDescMap.end(); iter++)
		{
			if (newKeys.find(iter->first) == newKeys.end())
			{
				NFLogError(NF_LOG_DEFAULT, 0, "reload del old col, id:{}", iter->first);
			}
		}
	}

	m_astDescVec.clear();
	m_astDescMap.clear();
	m_llMinId = INVALID_ID;
	m_llMaxId = INVALID_ID;
	for (int i = 0; i < (int)table.e_activityeightactivityeight_list_size(); i++)
	{
		const proto_ff::E_ActivityEightActivityEight& desc = table.e_activityeightactivityeight_list(i);
		if ((desc.has_id() == false && desc.ByteSize() == 0) || desc.id() < 0)
		{
			NFLogError(NF_LOG_DEFAULT, 0, "the desc no value, {}", desc.Utf8DebugString());
			continue;
		}

		if (m_llMinId == INVALID_ID || desc.id() < m_llMinId)
		{
			m_llMinId = desc.id();
		}

		if (m_llMaxId == INVALID_ID || desc.id() > m_llMaxId)
		{
			m_llMaxId = desc.id();
		}
	}

	for (int i = 0; i < (int)table.e_activityeightactivityeight_list_size(); i++)
	{
		const proto_ff::E_ActivityEightActivityEight& desc = table.e_activityeightactivityeight_list(i);
		if ((desc.has_id() == false && desc.ByteSize() == 0) || desc.id() < 0)
		{
			continue;
		}

		//NFLogTrace(NF_LOG_DEFAULT, 0, "{}", desc.Utf8DebugString());
		if (!IsInRangeId(desc.id()))
		{
			NFLogError(NF_LOG_DEFAULT, 0, "the desc:{} id:{} not in range [min, max]", GetClassName(), desc.id());
			return -1;
		}
		if (m_astDescMap.find(desc.id()) != m_astDescMap.end())
		{
			NFLogError(NF_LOG_DEFAULT, 0, "the desc:{} id:{} exist", GetClassName(), desc.id());
			return -1;
		}
		CHECK_EXPR_ASSERT(m_astDescVec.size() < m_astDescVec.max_size(), -1, "m_astDescVec Space Not Enough");
		m_astDescVec.emplace_back();
		auto pDesc = &m_astDescVec.back();
		CHECK_EXPR_ASSERT(pDesc, -1, "m_astDescVec Insert Failed desc.id:{}", desc.id());
		pDesc->FromPb(desc);
		CHECK_EXPR_ASSERT(m_astDescMap.size() < m_astDescMap.max_size(), -1, "m_astDescMap Space Not Enough");
		m_astDescMap.insert(std::make_pair(desc.id(), m_astDescVec.size()-1));
		CHECK_EXPR_ASSERT(GetDesc(desc.id()) == pDesc, -1, "GetDesc != pDesc, id:{}", desc.id());
	}

	m_groupIdIndexMap.clear();
	m_typeIndexMap.clear();
	for(int i = 0; i < (int)m_astDescVec.size(); i++)
	{
		auto pDesc = &m_astDescVec[i];
		{
			auto iter = m_groupIdIndexMap.insert(std::make_pair(pDesc->group_id, i));
			CHECK_EXPR_ASSERT(iter != m_groupIdIndexMap.end(), -1, "m_groupIdIndexMap.insert Failed pDesc->group_id:{}, space not enough", pDesc->group_id);
		}
		{
			auto iter = m_typeIndexMap.insert(std::make_pair(pDesc->type, i));
			CHECK_EXPR_ASSERT(iter != m_typeIndexMap.end(), -1, "m_typeIndexMap.insert Failed pDesc->type:{}, space not enough", pDesc->type);
		}
	}
	if (IsReloading())
	{
		ActivityEightDescEx::Instance()->SetReLoading(true);
	}
	NFLogTrace(NF_LOG_DEFAULT, 0, "load {}, num={}", iRet, table.e_activityeightactivityeight_list_size());
	return 0;
}

int ActivityEightActivityEightDesc::CheckWhenAllDataLoaded()
{
	return 0;
}

std::vector<const E_ActivityEightActivityEight*> ActivityEightActivityEightDesc::GetDescByGroupId(int64_t groupId) const
{
	std::vector<const E_ActivityEightActivityEight*> m_vec;
	auto iter_pair = m_groupIdIndexMap.equal_range(groupId);
	for(auto iter = iter_pair.first; iter != iter_pair.second; iter++)
	{
		auto pDesc = GetDescByIndex(iter->second);
		CHECK_EXPR_CONTINUE(pDesc, "key:{} GetDesc error:{}", groupId, iter->second);
		m_vec.push_back(pDesc);
	}
	return m_vec;
}

std::vector<const E_ActivityEightActivityEight*> ActivityEightActivityEightDesc::GetDescByType(int64_t type) const
{
	std::vector<const E_ActivityEightActivityEight*> m_vec;
	auto iter_pair = m_typeIndexMap.equal_range(type);
	for(auto iter = iter_pair.first; iter != iter_pair.second; iter++)
	{
		auto pDesc = GetDescByIndex(iter->second);
		CHECK_EXPR_CONTINUE(pDesc, "key:{} GetDesc error:{}", type, iter->second);
		m_vec.push_back(pDesc);
	}
	return m_vec;
}

